<?php
Zend_Loader::loadClass('Application_Form');
Zend_Loader::loadClass('Application_Form_Element_PaneControls');

class Application_Form_Vetri extends Application_Form
{
	protected $_formDecorators = array(
			'FormElements',
			'Form',
			array('HtmlTag', array('tag' => 'div', 'id' => 'form-wrap')),
	);

	protected $_displaygroupDecorators = array(
			'FormElements',
			'Fieldset',
	);

	protected $_elementDecorators = array(
			'viewHelper',
			array(array('htmltag1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-span')),
			array('Label', array('tag' => 'div', 'tagClass' => 'form-span'))
	);

	public function __construct() {
		parent::__construct();
		$this->addJavascript("/validator/jquery.validationEngine.js");
		$this->addJavascript("/validator/languages/jquery.validationEngine-en.js");
		$this->addStylesheet("/validator/validationEngine.jquery.css");
		$this->addJavascript("/dataTables/jquery.dataTables.min.js");
		$this->addStylesheet("/dataTables/jquery.dataTables.css");
		$this->addStylesheet("/dataTables/demo_table_jui.css");
		$this->addStylesheet("/dataTables/fix.css");
	}

	public function run() {
		
		$request = Zend_Controller_Front::getInstance()->getRequest();
		if (!$request->isXmlHttpRequest()) {
			// Contesto non-AJAX
			$this->_initForm();
			$this->_view->browserForm = $this->_form->getSubForm("browser")->render($this->_view);
			$this->_view->detailForm = $this->_form->getSubForm("detail")->render($this->_view);
			return;

		} else {
			// Contesto AJAX
			$layout = Zend_Layout::getMvcInstance();
			$layout->disableLayout();

			$ret = "";
			if ($request->isPost()) {
				// POST data
				foreach ($request->getParams() as $paramName => $paramValue) {
					$urlParams[$paramName] = urlencode($paramValue);
				}
				$this->_initForm();
				if ($this->_form->getSubForm("detail")->isValid($request->getParams())) {
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					$redirector->setGotoRoute($urlParams, 'ajax');
					$redirector->redirectAndExit();
				} else {
					$messages = $this->_form->getSubForm("detail")->getMessages();
					if (array_key_exists('pane_name', $messages)) {
						array_unshift($messages['pane_name'], "Corregere gli errori segnalati");
					} else {
						$messages['pane_name'] = array ("generic" => "Corregere gli errori segnalati");
					}
					$ret = array("esito" => -1, "id" => $request->getParam('id'), "messages" => $messages);
				}
			} else {
				// Servizi ausiliari form
				switch ($request->getParam('function')) {
					default:
					$ret = null;
				}
			}
			echo Zend_Json::encode($ret);
		}
		
	}

	protected function _initForm()
	{
		$browserForm = new Zend_Form(array("id" => "browser"));
		$detailForm = new Zend_Form(array("id" => "detail"));
		$this->_form->addSubForm($browserForm, "browser");
		$this->_form->addSubForm($detailForm, "detail");
		
		///// TOP (dataTables) /////
		
		Zend_Loader::loadClass('Panes');
		$vetri = new Panes();
		$recordSet = $vetri->select()->from($vetri, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->panes = $vetri->fetchAll($recordSet)->toArray();

		///// BOTTOM /////

		$campo1 = new Zend_Form_Element_Hidden('pane_order');
		$campo1->setValue(10); //($value)  setOptions(array("value" => 10));

		$campo2 = new Zend_Form_Element_Text('pane_name');
		$campo2->setLabel('Nome del vetro')
		->setOptions(array(
				"id" => "pane_name",
				"order" => 3,
				"size" => 60,
				"class" => "validate[required]"
		))
		->setRequired(true);
		;

		$campo3 = new Application_Form_Element_PaneControls("pane_controls");
		$campo3->setOptions(array(
				"id" => "panes_controls",
				"order" => 2
		));
				
		$campo4 = new Zend_Form_Element_Text('pane_source');
		$campo4->setLabel('Fonte')
		->setOptions(array(
				"id" => "pane_source",
				"order" => 4,
				"size" => 60
		));
		
		$campo5 = new Zend_Form_Element_Text('pane_descr');
		$campo5->setLabel('Descrizione')
		->setOptions(array(
				"id" => "panes_descr",
				"order" => 5,
				"size" => 60
		));
		
		$campo6 = new Zend_Form_Element_Text('pane_category');
		$campo6->setLabel('Categoria')
		->setOptions(array(
				"id" => "pane_category",
				"order" => 6,
				"size" => 60
		));

		$campo7 = new Zend_Form_Element_Text('pane_igdbname');
		$campo7->setLabel('IGDB Name')
		->setOptions(array(
				"id" => "pane_igdbname",
				"order" => 7,
				"size" => 60
		));

		$campo8 = new Zend_Form_Element_Text('pane_nfrcid');
		$campo8->setLabel('NFRC ID')
		->setOptions(array(
				"id" => "pane_nfrcid",
				"order" => 8,
				"size" => 60,
				"class" => "validate[custom[number]]"
		));
		
		$campo9 = new Zend_Form_Element_Text('pane_datatype');
		$campo9->setLabel('Data type')
		->setOptions(array(
				"id" => "pane_datatype",
				"order" => 9,
				"size" => 60
		));
		
		$campo10 = new Zend_Form_Element_Text('pane_thickness');
		$campo10->setLabel('Spessore (mm)')
		->setOptions(array(
				"id" => "pane_thickness",
				"order" => 10,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		

		$campo11 = new Zend_Form_Element_Text('pane_conductivity');
		$campo11->setLabel('Conducibilità (W/m K)')
		->setOptions(array(
				"id" => "pane_conductivity",
				"order" => 11,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo12 = new Zend_Form_Element_Text('pane_solartransmittance');
		$campo12->setLabel('Trasmissione solare')
		->setOptions(array(
				"id" => "pane_solartransmittance",
				"order" => 12,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));


		$campo13 = new Zend_Form_Element_Text('pane_frontsolarreflectance');
		$campo13->setLabel('Riflessione solare esterna')
		->setOptions(array(
				"id" => "pane_frontsolarreflectance",
				"order" => 13,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
//
		$campo14 = new Zend_Form_Element_Text('pane_backsolarreflectance');
		$campo14->setLabel('Riflessione solare interna')
		->setOptions(array(
				"id" => "pane_backsolarreflectance",
				"order" => 14,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo15 = new Zend_Form_Element_Text('pane_visibletransmittance');
		$campo15->setLabel('Trasmissione visibile')
		->setOptions(array(
				"id" => "pane_visibletransmittance",
				"order" => 15,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo16 = new Zend_Form_Element_Text('pane_frontvisiblereflectance');
		$campo16->setLabel('Riflessione visibile esterna ')
		->setOptions(array(
				"id" => "pane_frontvisiblereflectance",
				"order" => 16,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo17 = new Zend_Form_Element_Text('pane_backvisiblereflectance');
		$campo17->setLabel('Riflessione visibile interna')
		->setOptions(array(
				"id" => "pane_backvisiblereflectance",
				"order" => 17,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo18 = new Zend_Form_Element_Text('pane_infraredtransmittance');
		$campo18->setLabel('Trasmissione infrarossi')
		->setOptions(array(
				"id" => "pane_infraredtransmittance",
				"order" => 18,
				"size" => 60,
				"class" => "validate[required,custom[number]]",
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo19 = new Zend_Form_Element_Text('pane_frontinfraredreflectance');
		$campo19->setLabel('Riflessione esterna infrarossi (emissività)')
		->setOptions(array(
				"id" => "pane_frontinfraredreflectance",
				"order" => 19,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo20 = new Zend_Form_Element_Text('pane_backinfraredreflectance');
		$campo20->setLabel('Riflessione interna infrarossi (emissività)')
		->setOptions(array(
				"id" => "pane_backinfraredreflectance",
				"order" => 20,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo21 = new Zend_Form_Element_Text('pane_spectraldata');
		$campo21->setLabel('Dati spettrali')
		->setOptions(array(
				"id" => "pane_spectraldata",
				"order" => 21,
				"size" => 60
		));
		
		$campo22 = new Zend_Form_Element_Hidden('pane_category_id');
		$campo22->setOptions(array(
				"id" => "pane_category_id",
		));
		

		// Adding form elements to form
		$this->_form->getSubForm("detail")->addElement($campo1);
		$this->_form->getSubForm("detail")->addElement($campo3);
		$this->_form->getSubForm("detail")->addElement($campo2);
		$this->_form->getSubForm("detail")->addElement($campo4);
		$this->_form->getSubForm("detail")->addElement($campo5);
		$this->_form->getSubForm("detail")->addElement($campo6);
		$this->_form->getSubForm("detail")->addElement($campo7);
		$this->_form->getSubForm("detail")->addElement($campo8);
		$this->_form->getSubForm("detail")->addElement($campo9);
		$this->_form->getSubForm("detail")->addElement($campo10);
		$this->_form->getSubForm("detail")->addElement($campo11);
		$this->_form->getSubForm("detail")->addElement($campo12);
		$this->_form->getSubForm("detail")->addElement($campo13);
		$this->_form->getSubForm("detail")->addElement($campo14);
		$this->_form->getSubForm("detail")->addElement($campo15);
		$this->_form->getSubForm("detail")->addElement($campo16);
		$this->_form->getSubForm("detail")->addElement($campo17);
		$this->_form->getSubForm("detail")->addElement($campo18);
		$this->_form->getSubForm("detail")->addElement($campo19);
		$this->_form->getSubForm("detail")->addElement($campo20);
		$this->_form->getSubForm("detail")->addElement($campo21);
		$this->_form->getSubForm("detail")->addElement($campo22);
		
		
		// Elementi hidden non devono avere decorators
		$elements = $this->_form->getSubForm("detail")->getElements();
		foreach ($elements as $elem) {
			if ($elem instanceof Zend_Form_Element_Hidden)
				$elem->setDecorators(array('ViewHelper'));
		}
		
		// Datasources
		Zend_Loader::loadClass('PaneCategories');
		$categorie = new PaneCategories();
		$recordSet = $categorie->select()->from($categorie, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->pane_categories = $categorie->fetchAll($recordSet)->toArray();
		
	}
}

